﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.Classes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.Generic;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using SharedCode;

namespace IndexMarkSilverlightClient.Views
{
    public class RespAnswersViewModel: ViewModelBase
    {

        #region DataMembers

        IndexMarkServiceClient client = new IndexMarkServiceClient();

        RespondentAnswers respAnswers = new RespondentAnswers()
            {
                Answers = new ObservableCollection<Answer>()
            };

        public RespondentAnswers RespAnswers
        {
            get 
            {
                return respAnswers; 
            }
            set
            {
                respAnswers = value;
                PropertyChangedInit(()=>this.RespAnswers);
            }
        }

        public Answer AnswerForEdit
        {
            get
            {
                foreach (Answer A in RespAnswers.Answers)
                {
                    if (A.IsSelected)
                    {
                        return A;
                    }
                }

                return null;
            }
            set
            {
                for(int i = 0; i < this.RespAnswers.Answers.Count; i++)
                {
                    Answer A = RespAnswers.Answers[i];

                    if (A.IsSelected)
                    {
                        A = value;
                    }
                }

                RespAnswers.IsEdited = true;
                OrderedAnswers();
                PropertyChangedInit(() => this.AnswerForEdit);
                PropertyChangedInit(() => this.RespAnswers);
                PropertyChangedInit(() => this.IsCorrect);
            }
        }

        private Term currentTerm = null;

        public Term CurrentTerm
        {
            get
            {
                return currentTerm;
            }
            set
            {
                currentTerm = value;
            }
        }

        private Characteristic currentChar = null;

        public Characteristic CurrentChar
        {
            get
            {
                return currentChar;
            }
            set
            {
                currentChar = value;
                PropertyChangedInit(()=>this.CurrentChar);
            }
        }

        public bool IsHasDelete
        {
            get
            {
                foreach (Answer A in RespAnswers.Answers)
                {
                    if (A.IsSelected)
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        public bool AllAnswersSelected
        {
            get
            {
                foreach (Answer A in RespAnswers.Answers)
                {
                    if (!A.IsSelected) return false;
                }
                return true;
            }
        }

        public bool AllAnswersUnSelected
        {
            get
            {
                foreach (Answer A in RespAnswers.Answers)
                {
                    if (A.IsSelected) return true;
                }
                return false;
            }
        }

        public bool IsHasEdit
        {
            get
            {
                int countSelected = 0;
                foreach (Answer A in RespAnswers.Answers)
                {
                    if (A.IsSelected)
                    {
                        ++countSelected;
                    }
                }

                //редактирование возможно, если выделен строго 1 элемент
                return countSelected == 1;
            }
        }

        public bool IsGraduated
        {
            get
            {
                return currentChar.IsGraduated;
            }
        }

        public bool IsCorrect
        {
            get
            {
                return RespAnswers.Correct;
            }
        }
        #endregion

        public RespAnswersViewModel()
        {
            client.GetAnswersByRespCompleted += new EventHandler<GetAnswersByRespCompletedEventArgs>(client_GetAnswersByRespCompleted);
            client.SaveAnswersCompleted += new EventHandler<AsyncCompletedEventArgs>(client_SaveAnswersCompleted);
            this.RespAnswers.Answers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Answers_CollectionChanged);
        }

        void Answers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RespAnswers.IsEdited = true;
            OrderedAnswers();
            PropertyChangedInit(() => this.RespAnswers);
            PropertyChangedInit(() => this.IsCorrect);
        }

        void client_SaveAnswersCompleted(object sender, AsyncCompletedEventArgs e)
        {
            LoadCompletedInit(typeof(bool));
        }

        public void LoadAnswers()
        {
            client.GetAnswersByRespAsync(this.currentChar.ID, this.respAnswers.Resp.IDRespondent);
        }

        void client_GetAnswersByRespCompleted(object sender, GetAnswersByRespCompletedEventArgs e)
        {
            RespAnswers = e.Result;
            OrderedAnswers();
            PropertyChangedInit(() => this.IsCorrect);
            PropertyChangedInit(() => this.RespAnswers);
            PropertyChangedInit("AllAnswersSelected");
            LoadCompletedInit(typeof(Answer));
        }

        public void OrderedAnswers()
        {
            if (RespAnswers.Answers == null) return;
            int i = 1;
            foreach (Answer A in RespAnswers.Answers)
            {
                if (A.State != StateObject.Deleted)
                {
                    A.Order = i;
                    i++;
                }
            }
        }

        public void SaveAnswers()
        {
            client.SaveAnswersAsync(this.RespAnswers);
        }

        public void DeleteAnswers()
        {
            foreach (Answer A in respAnswers.Answers)
            {
                if (A.IsSelected)
                {
                    A.State = StateObject.Deleted;
                    A.IsSelected = false;
                }
            }

            RespAnswers.IsEdited = true;
            OrderedAnswers();
            PropertyChangedInit(() => this.RespAnswers);
            PropertyChangedInit(() => this.IsCorrect);
            PropertyChangedInit("IsHasDelete");
            PropertyChangedInit("IsHasEdit");
        }

        public void SelectAnswers(bool Selected)
        {
            foreach (Answer A in respAnswers.Answers)
            {
                A.IsSelected = Selected;
                PropertyChangedInit(() => this.RespAnswers);
            }

            PropertyChangedInit("IsHasDelete");
            PropertyChangedInit("IsHasEdit");
            PropertyChangedInit("AllAnswersSelected");
        }

        public void SelectAnswer(Answer A, bool Selected)
        {
            A.IsSelected = Selected;
            PropertyChangedInit("IsHasDelete");
            PropertyChangedInit("IsHasEdit");
            PropertyChangedInit("AllAnswersSelected");
        }

        public void AddAnswer(Answer A)
        {
            RespAnswers.IsEdited = true;
            RespAnswers.Answers.Add(A);
            OrderedAnswers();
            PropertyChangedInit(() => this.RespAnswers);
            PropertyChangedInit(() => this.IsCorrect);
        }
    }
}
